# coding=utf-8
import time
import configparser
import os
import subprocess
import ctypes
import tkinter
import tkinter.messagebox
import win32api
import json
import urllib.parse
import urllib.request

# timestr = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
# print('--processing %s' % (timestr))

def readConfig():
    conf = configparser.ConfigParser()
    conf.read('C:/patrolService.conf')
    resultdir = conf.get('default','resultdir')
    filepath = resultdir + 'result.txt'
    print(filepath)

def formatstr():
    values = [
        ('cid001','mid001','2018-03-01 12:00:23'),
        ('cid002','mid002','2018-03-01 12:01:23')
        ]
    result_data = ''
    for item in values:
        result_data += 'CardID:%s,MeterID:%s,Time:%s \n' % (item[0],item[1],str(item[2]))
    print(result_data)

def getFileModifiedTime(filepath):
    filepath = 'E:/Git_Space/patrol 6.0.73/Patrol/patroldb.db'
    file_modified_time = time.localtime(os.path.getmtime(filepath))
    print(file_modified_time)
    return time.strftime('%Y-%m-%d %H:%M:%S',file_modified_time)

def testdict():
    dd = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
    for key in dd:
        print('key:%s, value:%s' % (key,str(dd[key])))

def popWindow():
    msg = u'数据同步完成！请前往系统查看'
    title = u'信息提示'
    ctypes.windll.user32.MessageBoxA(0,msg.encode('gb2312'),title.encode('gb2312'),0)

def popWindow2():
    msg = u'数据同步完成！请前往系统查看'
    title = u'信息提示'
    tkinter.messagebox.showinfo(title,msg)

def openApp():
    path = '‪C:/WINDOWS/system32/notepad.exe'
    win32api.ShellExecute(0, 'open', path, '','',1)

def openDos():
    path = 'demo.bat'   
    subprocess.Popen(path,shell=True)

def sendData():
    url = 'http://localhost:8089/app/index/syncPatrolDeviceData'
    params = {
        'data':[
            {'name':'浮生六记','author':'沈复'},
            {'name':'浮生六记2','author':'沈复2'},
            {'name':'浮生六记3','author':'沈复3'},
        ]
    }

    data = bytes(urllib.parse.urlencode(params), encoding='utf8')
    response = urllib.request.urlopen(url, data=data)
    print(response.read().decode('utf-8'))


if __name__ == "__main__":
    # time = getFileModifiedTime('')
    # print(time)
    sendData()
    pass